using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VSND
    {
        public DataRow Convert2SND(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QuerySND(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KBSMANDT"] = " ".PadRight(3 ); 
         dwReturn["KBSWERKS"] = " ".PadRight(4 ); 
         dwReturn["KBSCONID"] = " ".PadRight(2 ); 
         dwReturn["KBSTGINR"] = " ".PadRight(4 ); 
         dwReturn["KBSTGMON"] = " ".PadRight(2 ); 
         dwReturn["KBSTGDAY"] = " ".PadRight(2 ); 
         dwReturn["KBSTGLNR"] = " ".PadRight(0 ); 
         dwReturn["KBSQUITT"] = " ".PadRight(1 ); 
         dwReturn["KBSPRTFL"] = " ".PadRight(1 ); 
         dwReturn["KBSTGTIM"] = " ".PadRight(4 ); 
         dwReturn["KBSSNDRP"] = " ".PadRight(2 ); 
         dwReturn["KBSTNONR"] = " ".PadRight(1 ); 
         dwReturn["KBSSWERK"] = " ".PadRight(4 ); 
         dwReturn["KBSSNONR"] = " ".PadRight(1 ); 
         dwReturn["KBSTGFREE"] = " ".PadRight(8 ); 
         dwReturn["KBSTNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBSSNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBSTAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBSSAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBSDAT01"] = " ".PadRight(1000 ); 
         dwReturn["KBSDAT02"] = " ".PadRight(1000 ); 
         dwReturn["KBSSTATU"] = " ".PadRight(2 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapSND(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveSND(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QuerySND(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapSND(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertSND(dwLocal, ITXDBVERSION);
            }
            UpdateSND(dwLocal);
        }
        public void InsertSND(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO SND(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QuerySND(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM SND WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM SND  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "SND";
            Return = Result;
            return Return;
        }
        public void DeleteSND(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM SND   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateSND(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE SND SET "
                    +"KBSMANDT= :KBSMANDT,"
                    +"KBSWERKS= :KBSWERKS,"
                    +"KBSCONID= :KBSCONID,"
                    +"KBSTGINR= :KBSTGINR,"
                    +"KBSTGMON= :KBSTGMON,"
                    +"KBSTGDAY= :KBSTGDAY,"
                    +"KBSTGLNR= :KBSTGLNR,"
                    +"KBSQUITT= :KBSQUITT,"
                    +"KBSPRTFL= :KBSPRTFL,"
                    +"KBSTGTIM= :KBSTGTIM,"
                    +"KBSSNDRP= :KBSSNDRP,"
                    +"KBSTNONR= :KBSTNONR,"
                    +"KBSSWERK= :KBSSWERK,"
                    +"KBSSNONR= :KBSSNONR,"
                    +"KBSTGFREE= :KBSTGFREE,"
                    +"KBSTNNAM= :KBSTNNAM,"
                    +"KBSSNNAM= :KBSSNNAM,"
                    +"KBSTAPPL= :KBSTAPPL,"
                    +"KBSSAPPL= :KBSSAPPL,"
                    +"KBSDAT01= :KBSDAT01,"
                    +"KBSDAT02= :KBSDAT02,"
                    +"KBSSTATU= :KBSSTATU,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KBSMANDT", OracleType.NVarChar).Value = dwData["KBSMANDT"].ToString(); 
          or1.Parameters.Add("KBSWERKS", OracleType.NVarChar).Value = dwData["KBSWERKS"].ToString(); 
          or1.Parameters.Add("KBSCONID", OracleType.NVarChar).Value = dwData["KBSCONID"].ToString(); 
          or1.Parameters.Add("KBSTGINR", OracleType.NVarChar).Value = dwData["KBSTGINR"].ToString(); 
          or1.Parameters.Add("KBSTGMON", OracleType.NVarChar).Value = dwData["KBSTGMON"].ToString(); 
          or1.Parameters.Add("KBSTGDAY", OracleType.NVarChar).Value = dwData["KBSTGDAY"].ToString(); 
          or1.Parameters.Add("KBSTGLNR", OracleType.NVarChar).Value = dwData["KBSTGLNR"].ToString(); 
          or1.Parameters.Add("KBSQUITT", OracleType.NVarChar).Value = dwData["KBSQUITT"].ToString(); 
          or1.Parameters.Add("KBSPRTFL", OracleType.NVarChar).Value = dwData["KBSPRTFL"].ToString(); 
          or1.Parameters.Add("KBSTGTIM", OracleType.NVarChar).Value = dwData["KBSTGTIM"].ToString(); 
          or1.Parameters.Add("KBSSNDRP", OracleType.NVarChar).Value = dwData["KBSSNDRP"].ToString(); 
          or1.Parameters.Add("KBSTNONR", OracleType.NVarChar).Value = dwData["KBSTNONR"].ToString(); 
          or1.Parameters.Add("KBSSWERK", OracleType.NVarChar).Value = dwData["KBSSWERK"].ToString(); 
          or1.Parameters.Add("KBSSNONR", OracleType.NVarChar).Value = dwData["KBSSNONR"].ToString(); 
          or1.Parameters.Add("KBSTGFREE", OracleType.NVarChar).Value = dwData["KBSTGFREE"].ToString(); 
          or1.Parameters.Add("KBSTNNAM", OracleType.NVarChar).Value = dwData["KBSTNNAM"].ToString(); 
          or1.Parameters.Add("KBSSNNAM", OracleType.NVarChar).Value = dwData["KBSSNNAM"].ToString(); 
          or1.Parameters.Add("KBSTAPPL", OracleType.NVarChar).Value = dwData["KBSTAPPL"].ToString(); 
          or1.Parameters.Add("KBSSAPPL", OracleType.NVarChar).Value = dwData["KBSSAPPL"].ToString(); 
          or1.Parameters.Add("KBSDAT01", OracleType.NVarChar).Value = dwData["KBSDAT01"].ToString(); 
          or1.Parameters.Add("KBSDAT02", OracleType.NVarChar).Value = dwData["KBSDAT02"].ToString(); 
          or1.Parameters.Add("KBSSTATU", OracleType.NVarChar).Value = dwData["KBSSTATU"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
